/*
Do-file to create sibling order dataset from BHPS.
*/

clear
set more off

***Start with  the original sample in 1991
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\aindall.dta", clear

***Keep all children (biological and others)
keep if inrange(ahgr2r, 4, 8)

keep ahid apno ahgsex pid aage ahgfno ahgmno afpid ampid

rename pid pid_kid
rename aage age_kid
rename ahgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename afpid pid_father
rename ampid pid_mother


***Add father's age 
rename apno id
rename ahgfno apno
merge m:1 ahid apno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\aindall.dta", keepusing(aage)

drop if _merge==2
drop _merge
rename aage age_father
rename apno ahgfno


***Add mother's age 
rename ahgmno apno

merge m:1 ahid apno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\aindall.dta", keepusing(aage)

drop if _merge==2
drop _merge
rename aage age_mother
rename apno ahgmno



***Drop twin families
* Drop twin families
gen occ = 1
bysort ahid age_kid: egen nocc = sum(occ) if ahid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id ahgfno ahgmno
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008 (last survey)
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+17
}
*

gen year = 1991

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1991.dta", replace



************************************************
*Add on new entrants from the surveys 1992-2008*
************************************************
******
*1992*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\bindall.dta", clear

***Keep all children (biological and others)
keep if inrange(bhgr2r, 4, 8)
keep if bnewhy==1 | bnewhy==3
keep bhid bpno bhgsex pid bage bhgfno bhgmno bfpid bmpid

rename pid pid_kid
rename bage age_kid
rename bhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename bfpid pid_father
rename bmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid bhid)
keep if _merge==3
drop _merge
recast long ahid bhid
replace ahid = bhid if ahid==-8
rename pid pid_kid

***Add father's age 
rename bpno id
rename bhgfno bpno
merge m:1 bhid bpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\bindall.dta", keepusing(bage)

drop if _merge==2
drop _merge
rename bage age_father
rename bpno bhgfno


***Add mother's age 
rename bhgmno bpno

merge m:1 bhid bpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\bindall.dta", keepusing(bage)

drop if _merge==2
drop _merge
rename bage age_mother
rename bpno bhgmno

***Drop sibling families
gen occ = 1
bysort bhid age_kid: egen nocc = sum(occ) if bhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id bhgfno bhgmno bhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+16
}
*

gen year = 1992

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1992.dta", replace



******
*1993*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\cindall.dta", clear

***Keep all children (biological and others)
keep if inrange(chgr2r, 4, 8)
keep if cnewhy==1 | cnewhy==3
keep chid cpno chgsex pid cage chgfno chgmno cfpid cmpid

rename pid pid_kid
rename cage age_kid
rename chgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename cfpid pid_father
rename cmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid chid)
keep if _merge==3
drop _merge
recast long ahid chid
replace ahid = chid if ahid==-8
rename pid pid_kid


***Add father's age 
rename cpno id
rename chgfno cpno
merge m:1 chid cpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\cindall.dta", keepusing(cage)

drop if _merge==2
drop _merge
rename cage age_father
rename cpno chgfno


***Add mother's age 
rename chgmno cpno

merge m:1 chid cpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\cindall.dta", keepusing(cage)

drop if _merge==2
drop _merge
rename cage age_mother
rename cpno chgmno

***Drop sibling families
gen occ = 1
bysort chid age_kid: egen nocc = sum(occ) if chid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id chgfno chgmno chid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+15
}
*

gen year = 1993

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1993.dta", replace


******
*1994*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\dindall.dta", clear

***Keep all children (biological and others)
keep if inrange(dhgr2r, 4, 8)
keep if dnewhy==1 | dnewhy==3
keep dhid dpno dhgsex pid dage dhgfno dhgmno dfpid dmpid

rename pid pid_kid
rename dage age_kid
rename dhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename dfpid pid_father
rename dmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid dhid)
keep if _merge==3
drop _merge
recast long ahid dhid
replace ahid = dhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename dpno id
rename dhgfno dpno
merge m:1 dhid dpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\dindall.dta", keepusing(dage)

drop if _merge==2
drop _merge
rename dage age_father
rename dpno dhgfno


***Add mother's age 
rename dhgmno dpno

merge m:1 dhid dpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\dindall.dta", keepusing(dage)

drop if _merge==2
drop _merge
rename dage age_mother
rename dpno dhgmno

***Drop sibling families
gen occ = 1
bysort dhid age_kid: egen nocc = sum(occ) if dhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id dhgfno dhgmno dhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+14
}
*

gen year = 1994

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1994.dta", replace

******
*1995*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\eindall.dta", clear

***Keep all children (biological and others)
keep if inrange(ehgr2r, 4, 8)
keep if enewhy==1 | enewhy==3
keep ehid epno ehgsex pid eage ehgfno ehgmno efpid empid

rename pid pid_kid
rename eage age_kid
rename ehgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename efpid pid_father
rename empid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid ehid)
keep if _merge==3
drop _merge
recast long ahid ehid
replace ahid = ehid if ahid==-8
rename pid pid_kid


***Add father's age 
rename epno id
rename ehgfno epno
merge m:1 ehid epno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\eindall.dta", keepusing(eage)

drop if _merge==2
drop _merge
rename eage age_father
rename epno ehgfno


***Add mother's age 
rename ehgmno epno

merge m:1 ehid epno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\eindall.dta", keepusing(eage)

drop if _merge==2
drop _merge
rename eage age_mother
rename epno ehgmno

***Drop sibling families
gen occ = 1
bysort ehid age_kid: egen nocc = sum(occ) if ehid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id ehgfno ehgmno ehid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+13
}
*

gen year = 1995

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1995.dta", replace


******
*1996*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\findall.dta", clear

***Keep all children (biological and others)
keep if inrange(fhgr2r, 4, 8)
keep if fnewhy==1 | fnewhy==3
keep fhid fpno fhgsex pid fage fhgfno fhgmno ffpid fmpid

rename pid pid_kid
rename fage age_kid
rename fhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename ffpid pid_father
rename fmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid fhid)
keep if _merge==3
drop _merge
recast long ahid fhid
replace ahid = fhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename fpno id
rename fhgfno fpno
merge m:1 fhid fpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\findall.dta", keepusing(fage)

drop if _merge==2
drop _merge
rename fage age_father
rename fpno fhgfno


***Add mother's age 
rename fhgmno fpno

merge m:1 fhid fpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\findall.dta", keepusing(fage)

drop if _merge==2
drop _merge
rename fage age_mother
rename fpno fhgmno

***Drop sibling families
gen occ = 1
bysort fhid age_kid: egen nocc = sum(occ) if fhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id fhgfno fhgmno fhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+12
}
*

gen year = 1996

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1996.dta", replace


******
*1997*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\gindall.dta", clear

***Keep all children (biological and others)
keep if inrange(ghgr2r, 4, 8)
keep if gnewhy==1 | gnewhy==3
keep ghid gpno ghgsex pid gage ghgfno ghgmno gfpid gmpid

rename pid pid_kid
rename gage age_kid
rename ghgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename gfpid pid_father
rename gmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid ghid)
keep if _merge==3
drop _merge
recast long ahid ghid
replace ahid = ghid if ahid==-8
rename pid pid_kid


***Add father's age 
rename gpno id
rename ghgfno gpno
merge m:1 ghid gpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\gindall.dta", keepusing(gage)

drop if _merge==2
drop _merge
rename gage age_father
rename gpno ghgfno


***Add mother's age 
rename ghgmno gpno

merge m:1 ghid gpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\gindall.dta", keepusing(gage)

drop if _merge==2
drop _merge
rename gage age_mother
rename gpno ghgmno

***Drop sibling families
gen occ = 1
bysort ghid age_kid: egen nocc = sum(occ) if ghid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id ghgfno ghgmno ghid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+11
}
*

gen year = 1997

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1997.dta", replace


******
*1998*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\hindall.dta", clear

***Keep all children (biological and others)
keep if inrange(hhgr2r, 4, 8)
keep if hnewhy==1 | hnewhy==3
keep hhid hpno hhgsex pid hage hhgfno hhgmno hfpid hmpid

rename pid pid_kid
rename hage age_kid
rename hhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename hfpid pid_father
rename hmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid hhid)
keep if _merge==3
drop _merge
recast long ahid hhid
replace ahid = hhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename hpno id
rename hhgfno hpno
merge m:1 hhid hpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\hindall.dta", keepusing(hage)

drop if _merge==2
drop _merge
rename hage age_father
rename hpno hhgfno


***Add mother's age 
rename hhgmno hpno

merge m:1 hhid hpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\hindall.dta", keepusing(hage)

drop if _merge==2
drop _merge
rename hage age_mother
rename hpno hhgmno

***Drop sibling families
gen occ = 1
bysort hhid age_kid: egen nocc = sum(occ) if hhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id hhgfno hhgmno hhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+10
}
*

gen year = 1998

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1998.dta", replace


******
*1999*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\iindall.dta", clear

***Keep all children (biological and others)
keep if inrange(ihgr2r, 4, 8)
keep if inewhy==1 | inewhy==3
keep ihid ipno ihgsex pid iage ihgfno ihgmno ifpid impid

rename pid pid_kid
rename iage age_kid
rename ihgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename ifpid pid_father
rename impid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid ihid)
keep if _merge==3
drop _merge
recast long ahid ihid
replace ahid = ihid if ahid==-8
rename pid pid_kid


***Add father's age 
rename ipno id
rename ihgfno ipno
merge m:1 ihid ipno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\iindall.dta", keepusing(iage)

drop if _merge==2
drop _merge
rename iage age_father
rename ipno ihgfno


***Add mother's age 
rename ihgmno ipno

merge m:1 ihid ipno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\iindall.dta", keepusing(iage)

drop if _merge==2
drop _merge
rename iage age_mother
rename ipno ihgmno

***Drop sibling families
gen occ = 1
bysort ihid age_kid: egen nocc = sum(occ) if ihid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id ihgfno ihgmno ihid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+9
}
*

gen year = 1999

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1999.dta", replace


******
*2000*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\jindall.dta", clear

***Keep all children (biological and others)
keep if inrange(jhgr2r, 4, 8)
keep if jnewhy==1 | jnewhy==3
keep jhid jpno jhgsex pid jage jhgfno jhgmno jfpid jmpid

rename pid pid_kid
rename jage age_kid
rename jhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename jfpid pid_father
rename jmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid jhid)
keep if _merge==3
drop _merge
recast long ahid jhid
replace ahid = jhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename jpno id
rename jhgfno jpno
merge m:1 jhid jpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\jindall.dta", keepusing(jage)

drop if _merge==2
drop _merge
rename jage age_father
rename jpno jhgfno


***Add mother's age 
rename jhgmno jpno

merge m:1 jhid jpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\jindall.dta", keepusing(jage)

drop if _merge==2
drop _merge
rename jage age_mother
rename jpno jhgmno

***Drop sibling families
gen occ = 1
bysort jhid age_kid: egen nocc = sum(occ) if jhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id jhgfno jhgmno jhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+8
}
*

gen year = 2000

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2000.dta", replace


******
*2001*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\kindall.dta", clear

***Keep all children (biological and others)
keep if inrange(khgr2r, 4, 8)
keep if knewhy==1 | knewhy==3
keep khid kpno khgsex pid kage khgfno khgmno kfpid kmpid

rename pid pid_kid
rename kage age_kid
rename khgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename kfpid pid_father
rename kmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid khid)
keep if _merge==3
drop _merge
recast long ahid khid
replace ahid = khid if ahid==-8
rename pid pid_kid


***Add father's age 
rename kpno id
rename khgfno kpno
merge m:1 khid kpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\kindall.dta", keepusing(kage)

drop if _merge==2
drop _merge
rename kage age_father
rename kpno khgfno


***Add mother's age 
rename khgmno kpno

merge m:1 khid kpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\kindall.dta", keepusing(kage)

drop if _merge==2
drop _merge
rename kage age_mother
rename kpno khgmno

***Drop sibling families
gen occ = 1
bysort khid age_kid: egen nocc = sum(occ) if khid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id khgfno khgmno khid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+7
}
*

gen year = 2001

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2001.dta", replace


******
*2002*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\lindall.dta", clear

***Keep all children (biological and others)
keep if inrange(lhgr2r, 4, 8)
keep if lnewhy==1 | lnewhy==3
keep lhid lpno lhgsex pid lage lhgfno lhgmno lfpid lmpid

rename pid pid_kid
rename lage age_kid
rename lhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename lfpid pid_father
rename lmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid lhid)
keep if _merge==3
drop _merge
recast long ahid lhid
replace ahid = lhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename lpno id
rename lhgfno lpno
merge m:1 lhid lpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\lindall.dta", keepusing(lage)

drop if _merge==2
drop _merge
rename lage age_father
rename lpno lhgfno


***Add mother's age 
rename lhgmno lpno

merge m:1 lhid lpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\lindall.dta", keepusing(lage)

drop if _merge==2
drop _merge
rename lage age_mother
rename lpno lhgmno

***Drop sibling families
gen occ = 1
bysort lhid age_kid: egen nocc = sum(occ) if lhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id lhgfno lhgmno lhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+6
}
*

gen year = 2002

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2002.dta", replace


******
*2003*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\mindall.dta", clear

***Keep all children (biological and others)
keep if inrange(mhgr2r, 4, 8)
keep if mnewhy==1 | mnewhy==3
keep mhid mpno mhgsex pid mage mhgfno mhgmno mfpid mmpid

rename pid pid_kid
rename mage age_kid
rename mhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename mfpid pid_father
rename mmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid mhid)
keep if _merge==3
drop _merge
recast long ahid mhid
replace ahid = mhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename mpno id
rename mhgfno mpno
merge m:1 mhid mpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\mindall.dta", keepusing(mage)

drop if _merge==2
drop _merge
rename mage age_father
rename mpno mhgfno


***Add mother's age 
rename mhgmno mpno

merge m:1 mhid mpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\mindall.dta", keepusing(mage)

drop if _merge==2
drop _merge
rename mage age_mother
rename mpno mhgmno

***Drop sibling families
gen occ = 1
bysort mhid age_kid: egen nocc = sum(occ) if mhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id mhgfno mhgmno mhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+5
}
*

gen year = 2003

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2003.dta", replace


******
*2004*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\nindall.dta", clear

***Keep all children (biological and others)
keep if inrange(nhgr2r, 4, 8)
keep if nnewhy==1 | nnewhy==3
keep nhid npno nhgsex pid nage nhgfno nhgmno nfpid nmpid

rename pid pid_kid
rename nage age_kid
rename nhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename nfpid pid_father
rename nmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid nhid)
keep if _merge==3
drop _merge
recast long ahid nhid
replace ahid = nhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename npno id
rename nhgfno npno
merge m:1 nhid npno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\nindall.dta", keepusing(nage)

drop if _merge==2
drop _merge
rename nage age_father
rename npno nhgfno


***Add mother's age 
rename nhgmno npno

merge m:1 nhid npno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\nindall.dta", keepusing(nage)

drop if _merge==2
drop _merge
rename nage age_mother
rename npno nhgmno

***Drop sibling families
gen occ = 1
bysort nhid age_kid: egen nocc = sum(occ) if nhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id nhgfno nhgmno nhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+4
}
*

gen year = 2004

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2004.dta", replace


******
*2005*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\oindall.dta", clear

***Keep all children (biological and others)
keep if inrange(ohgr2r, 4, 8)
keep if onewhy==1 | onewhy==3
keep ohid opno ohgsex pid oage ohgfno ohgmno ofpid ompid

rename pid pid_kid
rename oage age_kid
rename ohgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename ofpid pid_father
rename ompid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid ohid)
keep if _merge==3
drop _merge
recast long ahid ohid
replace ahid = ohid if ahid==-8
rename pid pid_kid


***Add father's age 
rename opno id
rename ohgfno opno
merge m:1 ohid opno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\oindall.dta", keepusing(oage)

drop if _merge==2
drop _merge
rename oage age_father
rename opno ohgfno


***Add mother's age 
rename ohgmno opno

merge m:1 ohid opno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\oindall.dta", keepusing(oage)

drop if _merge==2
drop _merge
rename oage age_mother
rename opno ohgmno

***Drop sibling families
gen occ = 1
bysort ohid age_kid: egen nocc = sum(occ) if ohid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id ohgfno ohgmno ohid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+3
}
*

gen year = 2005

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2005.dta", replace


******
*2006*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\pindall.dta", clear

***Keep all children (biological and others)
keep if inrange(phgr2r, 4, 8)
keep if pnewhy==1 | pnewhy==3
keep phid ppno phgsex pid page phgfno phgmno pfpid pmpid

rename pid pid_kid
rename page age_kid
rename phgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename pfpid pid_father
rename pmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid phid)
keep if _merge==3
drop _merge
recast long ahid phid
replace ahid = phid if ahid==-8
rename pid pid_kid


***Add father's age 
rename ppno id
rename phgfno ppno
merge m:1 phid ppno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\pindall.dta", keepusing(page)

drop if _merge==2
drop _merge
rename page age_father
rename ppno phgfno


***Add mother's age 
rename phgmno ppno

merge m:1 phid ppno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\pindall.dta", keepusing(page)

drop if _merge==2
drop _merge
rename page age_mother
rename ppno phgmno

***Drop sibling families
gen occ = 1
bysort phid age_kid: egen nocc = sum(occ) if phid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id phgfno phgmno phid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+2
}
*

gen year = 2006

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2006.dta", replace


******
*2007*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\qindall.dta", clear

***Keep all children (biological and others)
keep if inrange(qhgr2r, 4, 8)
keep if qnewhy==1 | qnewhy==3
keep qhid qpno qhgsex pid qage qhgfno qhgmno qfpid qmpid

rename pid pid_kid
rename qage age_kid
rename qhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename qfpid pid_father
rename qmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid qhid)
keep if _merge==3
drop _merge
recast long ahid qhid
replace ahid = qhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename qpno id
rename qhgfno qpno
merge m:1 qhid qpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\qindall.dta", keepusing(qage)

drop if _merge==2
drop _merge
rename qage age_father
rename qpno qhgfno


***Add mother's age 
rename qhgmno qpno

merge m:1 qhid qpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\qindall.dta", keepusing(qage)

drop if _merge==2
drop _merge
rename qage age_mother
rename qpno qhgmno

***Drop sibling families
gen occ = 1
bysort qhid age_kid: egen nocc = sum(occ) if qhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id qhgfno qhgmno qhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

***Set age to age in 2008
foreach var of varlist age_kid age_father age_mother {
replace `var' = `var'+1
}
*

gen year = 2007

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2007.dta", replace


******
*2008*
******
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\rindall.dta", clear

***Keep all children (biological and others)
keep if inrange(rhgr2r, 4, 8)
keep if rnewhy==1 | rnewhy==3
keep rhid rpno rhgsex pid rage rhgfno rhgmno rfpid rmpid

rename pid pid_kid
rename rage age_kid
rename rhgsex sex_kid
replace sex_kid = . if sex_kid==-9
rename rfpid pid_father
rename rmpid pid_mother

***Add household id
rename pid_kid pid
merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\xwaveid.dta", keepusing(ahid rhid)
keep if _merge==3
drop _merge
recast long ahid rhid
replace ahid = rhid if ahid==-8
rename pid pid_kid


***Add father's age 
rename rpno id
rename rhgfno rpno
merge m:1 rhid rpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\rindall.dta", keepusing(rage)

drop if _merge==2
drop _merge
rename rage age_father
rename rpno rhgfno


***Add mother's age 
rename rhgmno rpno

merge m:1 rhid rpno using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\rindall.dta", keepusing(rage)

drop if _merge==2
drop _merge
rename rage age_mother
rename rpno rhgmno

***Drop sibling families
gen occ = 1
bysort rhid age_kid: egen nocc = sum(occ) if rhid!=.
tab nocc
keep if nocc==1
drop occ nocc


***Drop wave specific id's and order
drop id rhgfno rhgmno rhid
order ahid pid_kid pid_father pid_mother


***Generate indicator for youngest parent
egen parent_youngest = rowmin(age_father age_mother)

gen year = 2008

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps2008.dta", replace


clear

*Append and erase temp files
use "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps1991.dta", clear
foreach num of numlist 1992(1)2008 {
append using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps`num'.dta"
}

save "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPSpooled.dta", replace

foreach num of numlist 1991(1)2008 {
erase "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Temp\bhps`num'.dta"
}



***Drop some reentering individuals
gen occ = year*-1+2100
bysort pid_kid: egen nocc = rank(occ), field
keep if nocc==1
drop occ nocc


***Generate indicator for family size
gen occ = 1
bysort ahid: egen famsize = sum(occ)
drop occ

***Generate indicators for sibling order
bysort ahid: egen siblingorder = rank(age_kid), field
*bysort pid_mother: egen siblingorder = rank(age_kid), field

gen sibling2 = .
replace sibling2 = 0 if inrange(siblingorder, 2, 15)
replace sibling2 = 1 if siblingorder==1


gen sibling3 = .
replace sibling3 = 1 if siblingorder==1
replace sibling3 = 2 if siblingorder==2
replace sibling3 = 3 if inrange(siblingorder, 3, 15)

***Generate indicator for youngest parent of oldest sibling in hh being younger than 41
gen occ = .
replace occ = 1 if sibling2==1 & parent_youngest<41
bysort ahid: egen parent_filter = sum(occ)
drop occ

keep if parent_filter==1


****************************************
*MERGE TO POLITICAL OUTCOMES - CHILDREN*
****************************************
rename pid_kid pid

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\aindresp.dta", keepusing(avote6)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\bindresp.dta", keepusing(bvote6 bvote7 boppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\cindresp.dta", keepusing(cvote6)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\dindresp.dta", keepusing(dvote6 doppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\eindresp.dta", keepusing(evote6 evote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\findresp.dta", keepusing(fvote6 foppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\gindresp.dta", keepusing(gvote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\hindresp.dta", keepusing(hvote7 hoppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\iindresp.dta", keepusing(ivote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\jindresp.dta", keepusing(jvote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\kindresp.dta", keepusing(kvote6 kvote7 koppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\lindresp.dta", keepusing(lvote6 lvote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\mindresp.dta", keepusing(mvote6 mvote7 moppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\nindresp.dta", keepusing(nvote6 nvote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\oindresp.dta", keepusing(ovote6 ovote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\pindresp.dta", keepusing(pvote6 pvote7 poppolb)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\qindresp.dta", keepusing(qvote6 qvote7)
drop if _merge==2
drop _merge

merge 1:1 pid using "D:\Data\ProjData\SiblingOrder\OtherSamples\BHPS\Data\rindresp.dta", keepusing(rvote6 rvote7)
drop if _merge==2
drop _merge

rename pid pid_kid




******************************************
*GENERATE POLITICAL INDICATORS - CHILDREN*
******************************************
***Average turnout across the waves
foreach x in b e g h i j k l m n o p q r {
gen `x'turnout_kid = .
replace `x'turnout_kid = 0 if `x'vote7==2
replace `x'turnout_kid = 1 if `x'vote7==1
}
*

egen turnout_kid = rowmean(bturnout eturnout gturnout gturnout iturnout jturnout kturnout lturnout mturnout nturnout oturnout pturnout qturnout rturnout)


***Average political interest (recoded to 0-10) across the waves
foreach x in a b c d e f k l m n o p q r {
replace `x'vote6 = . if `x'vote6<0
}
*
egen polintr_kid = rowmean(avote6 bvote6 cvote6 dvote6 evote6 fvote6 kvote6 lvote6 mvote6 nvote6 ovote6 pvote6 qvote6 rvote6)
replace polintr_kid = 10*(polintr_kid*-1+4)/3


***Confidence in government/external efficacy
foreach x in b d f h k m p {
replace `x'oppolb = . if `x'oppolb<0
}
*

egen gov_infl_kid = rowmean(boppolb doppolb foppolb hoppolb koppolb moppolb poppolb)
replace gov_infl_kid = 2.5*(gov_infl_kid-1)



***Drop some variables
foreach x in b d f h k m p {
drop `x'oppolb
}
*
foreach x in a b c d e f g h i j k l m n o p q r {
drop `x'vote*
}
*
drop parent_filter - rturnout_kid




***Save file for pooled analysis
*qui areg turnout_kid i.sibling2 i.age_kid sex_kid, absorb(ahid) cluster(ahid)
*keep if e(sample)
keep ahid turnout_kid siblingorder sibling2 age_kid sex_kid polintr_kid gov_infl_kid
rename ahid famid
rename turnout_kid turnout
rename siblingorder birthorder
rename sibling2 birthorder_dum
rename sex_kid female
rename age_kid age
rename polintr_kid polinterest
rename gov_infl_kid exteff
replace female = female-1
gen sample = "BHPS"
keep if inrange(birthorder, 1, 5)
qui: reghdfe turnout birthorder_dum female i.age, absorb(famid) cluster(famid)
sum turnout if e(sample)
gen turnout_average = r(mean)

save "D:\Data\ProjData\SiblingOrder\ReplicationBHPS data 191128.dta", replace

